package com.cheng.service.takedelivery.impl;

import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryStringQueryBuilder;
import org.elasticsearch.index.query.QueryStringQueryBuilder.Operator;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.index.query.WildcardQueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cheng.dao.takedelivery.WayBillDao;
import com.cheng.domain.takedelivery.WayBill;
import com.cheng.index.takedelivery.WayBillIndexDao;
import com.cheng.service.takedelivery.WayBillService;

@Service
@Transactional
public class WayBillServiceImpl implements WayBillService {

	@Autowired
	private WayBillDao wayBillDao;
	@Autowired
	private WayBillIndexDao wayBillIndexDao;
	@Override
	public void saveWayBill(WayBill model) {
		WayBill waybill = wayBillDao.findByWayBillNum(model.getWayBillNum());
		if (waybill.getSignStatus() != 1) {
			throw new RuntimeException("运单已经开始配送，无法修改！");
		}
		if (waybill != null) {
			Integer id = waybill.getId();
			try {
				BeanUtils.copyProperties(waybill, model);
				waybill.setId(id);
				//保存索引库
				wayBillIndexDao.save(waybill);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}else{
			//将运单设置为待发货
			model.setSignStatus(1);
			wayBillDao.save(model);
			//保存索引库
			wayBillIndexDao.save(model);
		}
	}

	@Override
	public Page<WayBill> findAllByPage(Pageable pageable) {
		
		return wayBillDao.findAll(pageable);
	}

	@Override
	public WayBill findWayBillByWayBillNum(String wayBillNum) {
		return wayBillDao.findByWayBillNum(wayBillNum);
	}

	@Override
	public Page<WayBill> findAllByPage(WayBill wayBill, Pageable pageable) {
		if (StringUtils.isBlank(wayBill.getWayBillNum())
				&& StringUtils.isBlank(wayBill.getSendAddress())
				&& StringUtils.isBlank(wayBill.getRecAddress())
				&& StringUtils.isBlank(wayBill.getSendProNum())
				&& (wayBill.getSignStatus() == null || wayBill.getSignStatus() == 0)) {
			return wayBillDao.findAll(pageable);
		}else{
			// 查询条件
			// must 条件必须成立 and
			// must not 条件必须不成立 not
			// should 条件可以成立 or
			BoolQueryBuilder queryBuilder = new BoolQueryBuilder();
			//订单号查询
			if (StringUtils.isNoneBlank(wayBill.getWayBillNum())) {
				//tremquery完全匹配
				QueryBuilder termQuery = new TermQueryBuilder("wayBillNum", wayBill.getWayBillNum());
				queryBuilder.must(termQuery);
			}
			//发货地查询
			if (StringUtils.isNoneBlank(wayBill.getSendAddress())) {
				//情况一：输入的条件长度小于词条长度，输入条件不需要分词
				QueryBuilder wildcardQuery = new WildcardQueryBuilder("sendAddress", "*"+wayBill.getSendAddress()+"*");
				//情况二：输入的条件长度大于词条长度，输入条件需要分词
				QueryStringQueryBuilder stringquery = new QueryStringQueryBuilder(wayBill.getSendAddress()).field("sendAddress").defaultOperator(Operator.AND);
				//两种情况取并集
				BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
				boolQueryBuilder.should(stringquery);
				boolQueryBuilder.should(wildcardQuery);
				//最终结果取并集
				queryBuilder.must(boolQueryBuilder);
			}
			//收货地查询
			if (StringUtils.isNoneBlank(wayBill.getRecAddress())) {
				//情况一：输入的条件长度小于词条长度，输入条件不需要分词
				QueryBuilder wildcardQuery = new WildcardQueryBuilder("recAddress", "*"+wayBill.getRecAddress()+"*");
				//情况二：输入的条件长度大于词条长度，输入条件需要分词
				QueryStringQueryBuilder stringquery = new QueryStringQueryBuilder(wayBill.getRecAddress()).field("recAddress").defaultOperator(Operator.AND);
				//两种情况取并集
				BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
				boolQueryBuilder.should(stringquery);
				boolQueryBuilder.should(wildcardQuery);
				//最终结果取并集
				queryBuilder.must(boolQueryBuilder);
			}
			if (StringUtils.isNoneBlank(wayBill.getSendProNum())) {
				// 速运类型 等值查询
				QueryBuilder termQuery = new TermQueryBuilder("sendProNum",
						wayBill.getSendProNum());
				queryBuilder.must(termQuery);
			}
			if (wayBill.getSignStatus() != null && wayBill.getSignStatus() != 0) {
				// 签收状态查询
				QueryBuilder termQuery = new TermQueryBuilder("signStatus",
						wayBill.getSignStatus());
				queryBuilder.must(termQuery);
			}
			//拼接查询条件
			SearchQuery searchQuery = new NativeSearchQuery(queryBuilder);
			//设置分页
			searchQuery.setPageable(pageable);
			return wayBillIndexDao.search(searchQuery);
		}
		
	}

	@Override
	public void syncIndex() {
		//同步索引库和运单库
		List<WayBill> list = wayBillDao.findAll();
		wayBillIndexDao.save(list);
	}

}
